const { verify } = require('../util/jwt')
const { jwtSecret } = require('../config/default');
const { User } = require('../model')

module.exports = async (req, res, next) => {
    //从请求头获取token数据
    let token = req.headers["authorization"];
    token = token?token.split('token ')[1] : null;
    if (!token) {
        return res.status(403).end()
    }
    //验证token
    try {
        const decoderToken = await verify(token, jwtSecret);
        req.user =await User.findById(decoderToken.userId);
        next()
    } catch (err) {
        return res.status(402).end()
    }
    //验证完后相应
}